﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HotelManagement.DTO;

namespace HotelManagement.DAO
{
    class RentDAO:DataProvider
    {
        public RentDAO():
            base()
        {

        }

        public bool Insert(RentDTO rent)
        {
            string strInsert = "insert into RENT(RoomID,UserName,RegisterDate,CheckInDate,CustomerID,Status,Note) values(";
            strInsert += rent.RoomID + ",";
            strInsert += "'" + rent.UserName + "',";
            strInsert += "'" + rent.RegisterDate + "',";
            strInsert += "'" + rent.CheckInDate + "',";
            strInsert += rent.CustomerID + ",";
            strInsert += rent.Status + ",";
            strInsert += "N'" + rent.Note + "')";
            return base._ExecuteQuery(strInsert);
        }

        public bool Update(RentDTO rent)
        {
            string strUpdate = "update RENT set ";
            strUpdate += "RENT.RoomID=" + rent.RoomID + ",";
            strUpdate += "RENT.UserName='" + rent.UserName + "',";
            strUpdate += "RENT.RegisterDate='" + rent.RegisterDate + "',";
            strUpdate += "RENT.CheckInDate='" + rent.CheckInDate + "',";
            strUpdate += "RENT.CustomerID = " + rent.CustomerID + ",";
            strUpdate += "RENT.Status = " + rent.Status + ",";
            strUpdate += "RENT.Note=N'" + rent.Note + "' ";
            strUpdate += " where RENT.RentID=" + rent.RentID;
            
            return base._ExecuteQuery(strUpdate);
        }
        public bool UpdateStatus(RentDTO rent)
        {
            string strUpdate = "update RENT set ";
            strUpdate += "RENT.Status = " + rent.Status;
            strUpdate += " where RENT.RentID=" + rent.RentID;

            return base._ExecuteQuery(strUpdate);
        }

        public DataTable Select()
        {
            string strSelect = "select * from rent";
            return base._Select(strSelect);
        }
        public RentDTO SelectRentFromID(int pRentID)
        {
            string sql = "select * from rent where rent.RentID = " + pRentID;
            DataTable tbl = base._Select(sql);
            DataRow r = tbl.Rows[0];
            RentDTO re = new RentDTO();
            re.RentID = int.Parse(r["RentID"].ToString());
            re.RoomID = int.Parse(r["RoomID"].ToString());
            re.UserName = r["UserName"].ToString();
            re.RegisterDate=DateTime.Parse(r["RegisterDate"].ToString());
            re.CheckInDate = DateTime.Parse(r["CheckInDate"].ToString());
            re.CustomerID = int.Parse(r["CustomerID"].ToString());
            re.Status = int.Parse(r["Status"].ToString());
            re.Note = r["Note"].ToString();
            return re;
        }
        public RentDTO SelectFromRoomIDAndStatus(int pRoomID, int pStatus)
        {
            if (pRoomID == 0)
            {
                return null;
            }
            string sql = "select * from rent where rent.roomid = " + pRoomID + " and rent.status = " + pStatus;
            DataTable tbl = base._Select(sql);
            if (tbl == null)
            {
                return null;
            }
            RentDTO rent = new RentDTO();
            rent.RentID = int.Parse(tbl.Rows[0]["RentID"].ToString());
            rent.RoomID = int.Parse(tbl.Rows[0]["RoomID"].ToString());
            rent.UserName = tbl.Rows[0]["UserName"].ToString();
            rent.RegisterDate = DateTime.Parse(tbl.Rows[0]["RegisterDate"].ToString());
            rent.CheckInDate = DateTime.Parse(tbl.Rows[0]["CheckInDate"].ToString());
            rent.CustomerID = int.Parse(tbl.Rows[0]["CustomerID"].ToString());
            rent.Status = int.Parse(tbl.Rows[0]["Status"].ToString());
            rent.Note = tbl.Rows[0]["Note"].ToString();
            return rent;
        }

        public DataTable GetBookInAndCheckInRent()
        {
            string strSelect = "select * from rent where rent.Status=" + HotelManagement.CheckInput.STATUS_BOOK_IN + " OR rent.Status=" + HotelManagement.CheckInput.STATUS_CHECK_IN;
            return base._Select(strSelect);
        }
        public DataTable SelectRoomOfARent(int pRentID)
        {
            string strSelect = "select * from room where room.roomid in ";
            strSelect += "(select roomid from rent where rent.rentid=" + pRentID + ")";
            return base._Select(strSelect);
        }
    }
}
